//
//  HJVoiceLeftCell.m
//  project
//
//  Created by hu on 2019/3/28.
//  Copyright © 2019 健健. All rights reserved.
//

#import "HJVoiceLeftCell.h"
#import "HJVocie.h"
#import <AVFoundation/AVFoundation.h>
#import "HJVociePlayer.h"

@interface HJVoiceLeftCell ()<AVAudioPlayerDelegate>
/*
 * 头像父视图
 */
@property (weak, nonatomic) IBOutlet UIView *headSubView;
/*
 * 头像图片
 */
@property (weak, nonatomic) IBOutlet UIImageView *headImage;
/*
 * 语音按钮
 */
@property (weak, nonatomic) IBOutlet UIButton *vocieBtn;
/*
 * 语音秒数
 */
@property (weak, nonatomic) IBOutlet UILabel *vocieNum;
/*
 * 语音按钮宽度
 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *vocieBtn_H;
@property (weak, nonatomic) IBOutlet UIButton *sendError;
@property (nonatomic,strong) RCMessage * vocieMsg;
@property (nonatomic,strong) AVAudioPlayer *Player;
@property (nonatomic,strong) HJGifImageView * gifImage;
@property (weak, nonatomic) IBOutlet UIImageView *yuyinImage;

@end


@implementation HJVoiceLeftCell


-(void)initGifImage {
    NSString * path = [[NSBundle mainBundle] pathForResource:@"语音播放left" ofType:@"gif"];
    _gifImage = [[HJGifImageView alloc] initWithGIFPath:path];
    _gifImage.frame = CGRectMake(self.yuyinImage.left, self.yuyinImage.top, self.yuyinImage.width, self.yuyinImage.height);
    [self.contentView addSubview:_gifImage];
    [self.contentView bringSubviewToFront:_gifImage];
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.vocieBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    // 用户图片 圆角
    [self.headImage rounded:(UIRectCornerAllCorners) radius:20];
    // 图片外的圆环
    [self.headSubView rounded:(UIRectCornerAllCorners) radius:20 borderColor:[UIColor blackColor] borderWidth:1];
    
    self.vocieBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [self initGifImage];
}

- (IBAction)vocieBtnClicked:(id)sender {
    RCVoiceMessage * vocieMsg = (RCVoiceMessage *)self.vocieMsg.content;
    if (_delegate && [_delegate respondsToSelector:@selector(voiceLeftMessageTaped:data:isRgiht:)]) {
        [_delegate voiceLeftMessageTaped:self data:vocieMsg.wavAudioData isRgiht:NO];
    }
}
-(void)setVoiceLeftPlayState:(LGVoiceLeftPlayState)voiceLeftPlayState {
    if (_voiceLeftPlayState != voiceLeftPlayState) {
        _voiceLeftPlayState = voiceLeftPlayState;
    }
    if (_voiceLeftPlayState == LGVoicePlayLeftStatePlaying) {
        self.yuyinImage.image = [UIImage new];
        [self.gifImage setHidden:NO];
        [self.gifImage startGIF];
    }else if (_voiceLeftPlayState == LGVoicePlayLeftStateDownloading) {
        [self.gifImage setHidden:YES];
        [self.gifImage stopGIF];
        self.yuyinImage.image = setImage(@"yuyin_chat");
    }else {
        self.yuyinImage.image = setImage(@"yuyin_chat");
        [self.gifImage setHidden:YES];
        [self.gifImage stopGIF];
    }
}
-(void)loadLeftVocieMsg:(RCMessage *)msg avatarUrl:(NSString *)url{
    self.vocieMsg = msg;
    //    if (msg.sentStatus == SentStatus_FAILED) {
    //        self.sendError.hidden = NO;
    //    }else {
    self.sendError.hidden = YES;
    //    }
    RCVoiceMessage * vocieMsg = (RCVoiceMessage *)msg.content;
    CGFloat width = SCREEN_WIDTH-85-100;
    if ([self vocieBtnWidthWithDuration:vocieMsg.duration] >= width) {
        self.vocieBtn_H.constant = width;
    }else {
        self.vocieBtn_H.constant = [self vocieBtnWidthWithDuration:vocieMsg.duration];
    }
    self.vocieNum.text = [NSString stringWithFormat:@"%ld''",vocieMsg.duration];
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:setImage(@"img_mydongtai_defualt_avatar")];
}
-(void)loadLeftVocieMsg:(RCMessage *)msg {
    self.vocieMsg = msg;
//    if (msg.sentStatus == SentStatus_FAILED) {
//        self.sendError.hidden = NO;
//    }else {
        self.sendError.hidden = YES;
//    }
    RCVoiceMessage * vocieMsg = (RCVoiceMessage *)msg.content;
    CGFloat width = SCREEN_WIDTH-85-100;
    if ([self vocieBtnWidthWithDuration:vocieMsg.duration] >= width) {
        self.vocieBtn_H.constant = width;
    }else {
        self.vocieBtn_H.constant = [self vocieBtnWidthWithDuration:vocieMsg.duration];
    }
    self.vocieNum.text = [NSString stringWithFormat:@"%ld''",vocieMsg.duration];
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:vocieMsg.senderUserInfo.portraitUri] placeholderImage:setImage(@"img_mydongtai_defualt_avatar")];
    
}
-(CGFloat)vocieBtnWidthWithDuration:(long)seconds {
    CGFloat maxWidth = SCREEN_WIDTH-85-100;
    return (CGFloat)(maxWidth / 60 * seconds) +80;
}
// 发送失败
- (IBAction)sendErrorBtnClicked:(UIButton *)sender {
    
}



@end
